package com.javaclimb.accounting.service.impl;

import com.javaclimb.accounting.dao.TransferDao;
import com.javaclimb.accounting.po.Account;
import com.javaclimb.accounting.po.Transfer;
import com.javaclimb.accounting.po.User;
import com.javaclimb.accounting.service.TransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TransferServiceImpl implements TransferService {
    @Autowired
    private TransferDao transferDao;

    @Override
    public List<User> getUsersByName(String beforeName, String afterName) {
        return transferDao.getUsersByName(beforeName,afterName);
    }

    @Override
    public List<Account> getAccount(Long beforeId, Long afterId) {
        return transferDao.getAccount(beforeId, afterId);
    }

    @Transactional
    @Override
    public void transfer(Transfer transfer, Account beforeAccount, Account afterAccount, double money) {
        //电子账户余额改变
        //增加
        transferDao.add(afterAccount.getAccId(), money);
        //减少
        transferDao.divide(beforeAccount.getAccId(), money);
        //增加转账记录
        transferDao.transfer(transfer);
    }

    @Override
    public List<User> getUsersByTel(String beforeTel, String afterTel) {
        return transferDao.getUsersByTel(beforeTel, afterTel);
    }
}
